package edu.ynmd.BMI.action;

import edu.ynmd.BMI.model.StuinfoEntity;
import edu.ynmd.BMI.service.ManageService;
import edu.ynmd.BMI.vo.BMIForm;
import edu.ynmd.BMI.vo.Result;
import edu.ynmd.BMI.vo.SfzForm;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class PublicAction {

    @Autowired
    private ManageService manageService;



   @PostMapping("getBmiValue")
    public Result getBimValue(@RequestBody BMIForm bmiForm)throws Exception{
       Float resultf=bmiForm.getTz()/(bmiForm.getSg()*bmiForm.getSg());
       String resultstr="";

       if (resultf<18.4){
           resultstr="偏瘦";
       }
       else if (resultf>=18.5&&resultf<=23.9){
           resultstr="正常";
       }
       else if (resultf>=24&&resultf<=27.9){
           resultstr="过重";
       }
       else {
           resultstr="肥胖";
       }

       Result r=new Result();
       r.setValue(resultf);
       r.setState(resultstr);

       switch (resultstr){
           case "偏瘦":
           r.setSugguest("多吃点");
           break;
           case "正常":
               r.setSugguest("别多吃，别少吃");
               break;
           case "过重":
               r.setSugguest("少吃点");
               break;
           case "肥胖":
               r.setSugguest("别吃了");
               break;
       }





       return r;
   }

    @GetMapping("testCrud")
    @ResponseBody
    public List<StuinfoEntity> testCrud() throws Exception{

        StuinfoEntity s=new StuinfoEntity();
        s.setStuinfoid("a128e0d8-b6e8-45ca-b39e-0f7b7648a3e5");
        s.setXm("王二");
        s.setSfzh("530101197408290010");
        s.setCsd("云南玉溪九龙池");
        s.setCsrq("1974-08-29");
        s.setXb("男");
        return manageService.getStuinfoEntityList();
       
    }


    @PostMapping("getSFZInfo")
    @ResponseBody
    public List<StuinfoEntity> getSFZInfo(@RequestBody SfzForm sfzForm) throws Exception{

       String xm=sfzForm.getXm();
       String sfzh=sfzForm.getSfzh();

       System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);

        String sfbm=sfzh.substring(0,2);
        System.out.println("省份编码是"+sfbm);
        String citybm=sfzh.substring(0,4);
        System.out.println("州市编码是"+citybm);
        String areabm=sfzh.substring(0,6);
        System.out.println("州县编码是"+areabm);



        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是"+csrqstr);

        String xbstr=sfzh.substring(16,17);
        System.out.println("性别位是"+xbstr);
        String xbname=manageService.getSexNameByStr(xbstr);
        System.out.println("性别是"+xbname);



        String sfstr=manageService.getProvinceNameByCode(sfbm);
        System.out.println("从数据库中取得的省份名称是"+sfstr);

        String citynamestr=manageService.getCityNameByCode(citybm);
        System.out.println("从数据库中取得的州市名称是"+citynamestr);

        String areanamestr=manageService.getAreaNameByCode(areabm);
        System.out.println("从数据库中取得的州区县名称是"+areanamestr);

        StuinfoEntity s=new StuinfoEntity();

        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfstr+citynamestr+areanamestr);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
        manageService.addStuinfoEntity(s);





       return manageService.getStuinfoEntityList();




    }


    @PostMapping("addStuinfoEntity")
    @ResponseBody
    public Boolean addStuinfoEntity(@RequestBody SfzForm sfzForm) throws Exception{

        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();

        System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);

        String sfbm=sfzh.substring(0,2);
        System.out.println("省份编码是"+sfbm);
        String citybm=sfzh.substring(0,4);
        System.out.println("州市编码是"+citybm);
        String areabm=sfzh.substring(0,6);
        System.out.println("州县编码是"+areabm);



        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是"+csrqstr);

        String xbstr=sfzh.substring(16,17);
        System.out.println("性别位是"+xbstr);
        String xbname=manageService.getSexNameByStr(xbstr);
        System.out.println("性别是"+xbname);



        String sfstr=manageService.getProvinceNameByCode(sfbm);
        System.out.println("从数据库中取得的名称是"+sfstr);

        String citynamestr=manageService.getCityNameByCode(citybm);
        System.out.println("从数据库中取得的州市名称是"+citynamestr);

        String areanamestr=manageService.getAreaNameByCode(areabm);
        System.out.println("从数据库中取得的州区县名称是"+areanamestr);

        StuinfoEntity s=new StuinfoEntity();

        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfstr+citynamestr+areanamestr);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
        return manageService.addStuinfoEntity(s);


    }


    @PostMapping("updateStuinfoEntity")
    @ResponseBody
    public Boolean updateStuinfo(@RequestBody SfzForm sfzForm)throws Exception{

        String xm=sfzForm.getXm();
        String sfzh=sfzForm.getSfzh();

        System.out.println("接收到的姓名是："+xm);
        System.out.println("接收到的身份证号是："+sfzh);

        String sfbm=sfzh.substring(0,2);
        System.out.println("省份编码是"+sfbm);
        String citybm=sfzh.substring(0,4);
        System.out.println("州市编码是"+citybm);
        String areabm=sfzh.substring(0,6);
        System.out.println("州县编码是"+areabm);



        String csrqstr=sfzh.substring(6,14);
        System.out.println("出生日期是"+csrqstr);

        String xbstr=sfzh.substring(16,17);
        System.out.println("性别位是"+xbstr);
        String xbname=manageService.getSexNameByStr(xbstr);
        System.out.println("性别是"+xbname);



        String sfstr=manageService.getProvinceNameByCode(sfbm);
        System.out.println("从数据库中取得的名称是"+sfstr);

        String citynamestr=manageService.getCityNameByCode(citybm);
        System.out.println("从数据库中取得的州市名称是"+citynamestr);

        String areanamestr=manageService.getAreaNameByCode(areabm);
        System.out.println("从数据库中取得的州区县名称是"+areanamestr);

        StuinfoEntity s=new StuinfoEntity();
        s.setStuinfoid(sfzForm.getStuinfoid()); ///指定主键
        s.setXm(xm);
        s.setSfzh(sfzh);
        s.setCsd(sfstr+citynamestr+areanamestr);
        s.setCsrq(csrqstr);
        s.setXb(xbname);
        return manageService.updateStuinfoEntity(s);//修改操作

    }


    @PostMapping("deleteStuinfoEntity")
    @ResponseBody
    public boolean deleteStuinfoEntity(@RequestBody StuinfoEntity stuinfoEntity)throws Exception{

       return manageService.deleteStuinfoEntity(stuinfoEntity.getStuinfoid());

    }


    @PostMapping("getSingleStuinfoEntity")
    @ResponseBody
    public StuinfoEntity getSingleStuinfoEntity(@RequestBody StuinfoEntity stuinfoEntity) throws Exception{
       return manageService.getSingleStuinfoEntity(stuinfoEntity.getStuinfoid());
    }


    @GetMapping("getStuinfoEntityList")
    @ResponseBody
    public List<StuinfoEntity> getStuoinfoEntityList() throws Exception{



       return manageService.getStuinfoEntityList();

    }




}
